﻿import QtQuick 2.12
import QtQuick.Controls 1.4
import Top 1.0
import TaoQuick 1.0

TextOnRect {
    id: rootItemId
    textItem.text: "客户端账号信息"
    radius: 10
    color: "transparent"
    Column {
        y: 30
        spacing: 5
        width: parent.width
        Row {
            spacing: 5
            anchors.horizontalCenter: parent.horizontalCenter
            CusTextField_Search {
                id: seachTextFiedld
                width: parent.width * 0.7
                height: 30
                function updataFilter() {
                    var index = filterComboBox.currentIndex
                    //如果 index为0 那么keyName就是ID所队友的Int值 如果index为1 那么就是name 以此类推
                    var keyName = index + StudentInfoModel.Id
                    SqlManager.attendanceInfoModel.fiterStr(keyName, seachTextFiedld.text)
                }

                onTextChanged: {//当文本发送变化的时就会调用这个函数
                    updataFilter()
                }
            }
            CusComboBox {
                id: filterComboBox
                width: 120
                height: 30
                model: ListModel {
                    id: filterModel
                    ListElement {
                        text: "账号";
                    }
                    ListElement {
                        text: "密码";
                    }
                    ListElement {
                        text: "状态";
                    }
                }
                //当选择的内容变了也要去搜索一下
                onCurrentIndexChanged: {
                    seachTextFiedld.updataFilter()
                }
            }
        }
        Row {
            anchors.horizontalCenter: parent.horizontalCenter
            TableView {
                id: tableview
                width: 850
                //能否编辑就看该标志位
                property bool editFlag: false
                clip: true
                //插7行以后高度就保持不变了
                height: rootItemId.height - 70
                visible: true
                backgroundVisible: false;
                //关联SqlManager.attendanceInfoModel这个类里面的所有信息
                model: GlobalManager.clientDataRecordSqlTableModel
                frameVisible: false;
                //防止他遮住外面的框
                z: 0
                //内容代理
                itemDelegate: Rectangle {
                    height: 40
                    width: parent.width
                    border.width: 1
                    //编辑该选项就有边框
                    border.color: {
                        //获得焦点(点击的时候)并且有编辑权限
                        if(editTextId.focus && !editTextId.readOnly) {
                            return "black"
                        }else {
                            return "transparent"
                        }
                    }
                    //用TextEdit来显示数据
                    TextEdit {
                        id: editTextId
                        anchors.verticalCenter: parent.verticalCenter
                        //左边偏移大小，因为贴左边的话字不好看
                        leftPadding: 10
                        selectByMouse: true
                        text: {
                            if(styleData.column == 4) {
                                if( styleData.value == 1) {
                                    color = "green"
                                    return "已连接"
                                }else {
                                    color = "red"
                                    return "未连接"
                                }
                            }else {
                                 return styleData.value
                            }
                        }

                        font.family: "Microsoft YaHei UI"
                        font.pixelSize: 20
                        readOnly: !tableview.editFlag
                        anchors.horizontalCenter: parent.horizontalCenter
                        onEditingFinished: {
                            focus = false
                            //SqlManager.attendanceInfoModel.modifyData(model.row, styleData.column, text)

                        }
                    }
                    CusButton {
                        width: parent.width*0.8
                        height: parent.height*0.8
                        anchors.left: parent.left
                        anchors.leftMargin: 25
                        visible: styleData.column == 3 && styleData.value == 3
                        text: "解冻账号"
                        onClicked: {
                            if(styleData.value == 3){
                                GlobalManager.clientDataRecordSqlTableModel.modifyData(styleData.row,styleData.column,0)
                            }
                        }
                    }
                }
                //行样式代理
                rowDelegate: Item {
                    height: 40
                    width: parent.width
                }

                //表头
                headerDelegate: Rectangle{
                    width: 233
                    height: 40
                    radius: 5
                    color: Qt.rgba(46/255,85/255,100/255,1);
                    border.color: "white"
                    border.width: 2
                    Text{
                        anchors.centerIn: parent
                        //左边偏移大小，因为贴左边的话字不好看
                        leftPadding: 10
                        //color: ""
                        text: styleData.value
                        font.family: "Microsoft YaHei UI"//字体格式
                        font.pixelSize: 20
                        font.bold: true
                    }
                }
                //与roleNames关联,qml内部自定义,通过role拿到int值,
                //相当与在roleNames那边把QByteArray映射成int
                TableViewColumn {
                    role: "accountNumber"//学号下面显示的内容
                    title: qsTr("账号")
                    width: 200
                }
                TableViewColumn {
                    role: "password"
                    title: qsTr("密码")
                    width: 200
                }
                TableViewColumn {
                    role: "registrationDate"
                    title: qsTr("注册日期")
                    width: 200
                }
                TableViewColumn {
                    role: "errorTimes"
                    title: qsTr("密码错误次数")
                    width: 150
                }
                TableViewColumn {
                    role: "userState"
                    title: qsTr("状态")
                    width: 100
                }
            }
            Column {
                //空出一个 因为下面的要从第一行开始对齐
                Item {
                    height: 40
                    width: 100
                }

                ListView {
                    height: rootItemId.height - 70
                    width: 230
                    clip: true
                    orientation: ListView.Vertical
                    model: GlobalManager.clientDataRecordSqlTableModel
                    interactive: false
                    delegate: Item {
                        width: 220
                        height: 40
                        Row {
                            spacing: 10
                            CusButton {
                                width: 50
                                height: 30
                                anchors.verticalCenter: parent.verticalCenter
                                text: "删除"
                                onClicked: {
                                    GlobalManager.clientDataRecordSqlTableModel.removeARow(model.index)
                                }
                            }
                            CusButton {
                                id:  openComBtnId
                                width: 150
                                height: 30
                                anchors.verticalCenter: parent.verticalCenter
                                text: "打开通讯页面"
                                enabled: {
                                    var connectState = GlobalManager.clientDataRecordSqlTableModel.getData(model.index, ClientDataRecordSqlTableModel.UserState)
                                    return connectState == "1"
                                }
                                //每1秒检查一次
                                Timer {
                                    interval: 1000
                                    repeat: true
                                    running: true
                                    onTriggered: {
                                        var connectState = GlobalManager.clientDataRecordSqlTableModel.getData(model.index, ClientDataRecordSqlTableModel.UserState)
                                        openComBtnId.enabled = connectState == "1"
                                    }
                                }

                                onClicked: {
                                    var account = GlobalManager.clientDataRecordSqlTableModel.getData(model.index, ClientDataRecordSqlTableModel.AccountNumber)
                                    comWindowId.title = "服务器:" + account + "聊天页面"
                                    comWindowId.userAccount = account
                                    comWindowId.show()
                                }
                            }
                        }
                        CommunicationWindow {
                            id: comWindowId
                            visible: false
                        }
                    }
                }
            }
        }
    }
}
